<template>
  <div>
    <BasicTable @register="register">
      <template #tableTitle>
        <Button @click="handleOpen(0)" type="primary"> 新增 </Button>
        <Upload class="ml-2" :showUploadList="false" :customRequest="handleImport">
          <Button> 导入 </Button>
        </Upload>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                label: '编辑',
                onClick: handleOpen.bind(null, record.templateID),
              },
              {
                label: '查看流程',
                onClick: handleView.bind(null, record.templateID),
              },
              {
                label: '导出',
                onClick: handleExport.bind(null, record),
              },
              {
                label: '删除',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record.templateID),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <BpmnModal v-model:visible="bpmnModalVisible" :templateID="templateID" @reload="reload" />
    <BpmnView @register="registerBpmnViewModal" />
  </div>
</template>
<script lang="ts" setup name="BpmnProcess">
  import { ref } from 'vue';
  import { Upload, Button } from 'ant-design-vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { schemaColumns, searchFormSchema } from './data';
  import { getTemplateDefinePageApi } from '/@/api/config-center/bpmn-process';
  import BpmnModal from './components/BpmnModal.vue';
  import BpmnView from './components/BpmnView/index.vue';
  import { useModal } from '/@/components/Modal';
  import {
    getTemplateDataApi,
    deleteTemplate,
    importTemplateApi,
  } from '/@/api/config-center/bpmn-process';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { downloadByUrl, setEncoded } from '/@/utils/file/download';

  const [register, { reload }] = useTable({
    api: getTemplateDefinePageApi,
    dataSource: [{ TemplateIsValid: true }],
    columns: schemaColumns,
    formConfig: {
      labelWidth: 100,
      schemas: searchFormSchema,
    },
    useSearchForm: true,
    showTableSetting: false,
    bordered: false,
    actionColumn: {
      width: 260,
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
    },
  });
  const bpmnModalVisible = ref<boolean>(false);
  const templateID = ref(0);
  function handleOpen(id) {
    templateID.value = id;
    bpmnModalVisible.value = true;
  }
  const [registerBpmnViewModal, { openModal: openBpmnViewModal }] = useModal();
  function handleView(id) {
    openBpmnViewModal(true, { TemplateID: id });
  }
  async function handleExport({ templatename, templateID }) {
    const data = await getTemplateDataApi({ TemplateID: templateID });
    const json = JSON.stringify(data);
    const { href, filename } = setEncoded('json', templatename, json!);
    downloadByUrl({ url: href, fileName: filename });
  }
  const { createMessage } = useMessage();

  function handleImport({ file }) {
    const reader = new FileReader();
    reader.readAsText(file);
    reader.onload = async function () {
      const bpmnData: any = this.result;
      //params.templateID = 0;
      const params = JSON.parse(bpmnData);
      await importTemplateApi({
        ...params,
        templateID: 0,
      });
      createMessage.success('操作成功');
      reload();
    };
  }

  async function handleDelete(id) {
    await deleteTemplate(id);
    createMessage.success('操作成功');
    reload();
  }
  // function handleImport(id) {
  //   templateID.value = id;
  //   bpmnModalVisible.value = true;
  // }
</script>
<style lang="less" scoped></style>
